<?php
// 连接数据库

$host = 'localhost:3306';
$user = 'root';
$pwd = 'root';
$dbname = 'kaola';

$con = mysqli_connect($host, $user, $pwd, $dbname);

// 测试数据库是否链接成功
if (!$con) {

  die('数据库连接失败');   //die这个方法结束整个php语句
}


// 数据返回给前端
$result = [];


// 前端携带参数  cart_id
$id = $_POST['id'];

//利用 explode 函数分割字符串到数组
$newId = explode(',', $id);

for ($i = 0; $i < count($newId); $i++) {
  if ($newId[$i]) {
    $sql = "DELETE FROM cart WHERE cart_id = $newId[$i]";
    $run = mysqli_query($con, $sql);
    if ($run > 0) {
      $result['status'] = true;
      $result['msg'] = '删除成功';
    } else {
      $result['status'] = false;
      $result['msg'] = '数据库错误';
    }
  } else {
    $result['status'] = false;
    $result['msg'] = '前端数据有误';
  }
}
echo (json_encode($result));
